const BeanUtils = {
    REG : /\s/,
    has: function (o, expression) {
        if (!o || !expression) return false;
        let a, b = o;
        if (!(expression instanceof Array)) {
            if (BeanUtils.REG.test (expression)) {
                console.warn ("there's white-space in expression, is it correct?");
            }
            a = expression.split ('.');
        } else {
            a = expression;
        }
        for (let i = 0; i < a.length; i ++) {
            if (!b.hasOwnProperty (a[i])) {
                return false;
            }
            b = b [a[i]];
        }

        return true;
    },
    get: function (o, expression) {
        if (!o || !expression)
            return undefined;
        let a, b = o, f;
        if (!(expression instanceof Array)) {
            if (BeanUtils.REG.test (expression)) {
                console.warn ("there's white-space in expression, is it correct?");
            }
            a = expression.split ('.');
        } else {
            a = expression;
        }
        f = a[a.length - 1];
        for (let i = 0; i < a.length - 1; i ++) {
            if (!b.hasOwnProperty (a[i])) {
                return undefined;
            }
            b = b [a[i]];
        }
        if (b && f) {
            return b[f];
        }
    },
    set: (o, k, v) => {
        if (!o || !k || !(typeof o === 'object')) return;
        if (BeanUtils.REG.test (k)) {
            console.warn ("there's white-space in expression, is it correct?");
        }
        let a = k.split ('.'), name = '', i, c = o, key = '';
        for (i = 0; i < a.length - 1; i ++) {
            name = a[i];
            if (key.length > 0) key += '.';
            key += name;
            if (!(name in c))
                c = c[name] = {};
            else
                c = c[name];
            if (!(typeof c === 'object')) {
                console.warn ('o[' + key + '] not an object!');
                return v;
            }
        }
        name = a[a.length -1];
        if (c) {
            if (v === undefined || v === null) {
                delete c[name];
            } else {
                c[name] = v;
            }
        }

        return v;
    },
};
export default BeanUtils;